package com.houtai.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.eduservice.config.OperationLogDesc;
import com.houtai.eduservice.entity.EduCourse;
import com.houtai.eduservice.entity.vo.CourseInfoVo;
import com.houtai.eduservice.entity.vo.CoursePublishVo;
import com.houtai.eduservice.entity.vo.CourseQuery;
import com.houtai.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-11-20
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
@Api(description = "课程管理")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //3.分页查询的方法
    @ApiOperation(value = "分页查询课程")
    @GetMapping("pageCourse/{current}/{limit}")
    public Result pageListTeacher(@PathVariable long current,
                                  @PathVariable long limit) {
        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        courseService.page(pageCourse,null);

        long total = pageCourse.getTotal();//总记录数
        List<EduCourse> records = pageCourse.getRecords(); //数据list集合

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return Result.ok().data("total",total).data("rows",records);
    }
    //课程列表
    @ApiOperation(value = "课程列表")
    @GetMapping
    public Result getCourseInfo(){
       List<EduCourse> list= courseService.list(null);
       return Result.ok().data("list",list);
    }
    //多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @OperationLogDesc(module = "课程模块",description = "查询课程列表")
    @PostMapping("getCourseInfo/{current}/{limit}")
    public Result getCourseInfo(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false)CourseQuery courseQuery)
    {
        //创建page对象
        Page<EduCourse> pageC = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(title)){
            //构建条件
            wrapper.like("title",title);

        }
        if(!StringUtils.isEmpty(status)){
            //构建条件
            wrapper.eq("status",status);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("gmt_create",begin);

        }
        if(!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("gmt_create",end);

        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        courseService.page(pageC,wrapper);

        long total = pageC.getTotal();//总记录数
        List<EduCourse> records = pageC.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }
    //添加课程基本信息
    @ApiOperation(value = "添加课程基本信息")
    @OperationLogDesc(module = "课程模块",description = "添加课程基本信息")
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody @Valid CourseInfoVo courseInfoVo){
        Integer id= courseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("courseId",id);
    }
    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    @ApiOperation(value = "查询课程基本信息")
    @OperationLogDesc(module = "课程模块",description = "查看课程基本信息")
    public Result getCourseInfo(@PathVariable Integer courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程基本信息
    @PostMapping("updateCourseInfo")
    @ApiOperation(value = "修改课程基本信息")
    @OperationLogDesc(module = "课程模块",description = "修改课程基本信息")
    public Result updateCourseInfo(@RequestBody @Valid CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();

    }
    //根据课程id课程确认信息
    @GetMapping("getPublishInfo/{id}")
    @ApiOperation(value = "根据课程id课程确认信息")
    @OperationLogDesc(module = "课程模块",description = "确认课程信息")
    public Result getPublishInfo(@PathVariable Integer id){
        CoursePublishVo coursePublishVo =courseService.publishCourseInfo(id);
        return Result.ok().data("publishCourse",coursePublishVo);

    }
    //最终发布，修改课程状态
    @PostMapping("publishCourse/{id}")
    @ApiOperation(value = "课程最终发布")
    @OperationLogDesc(module = "课程模块",description = "发布课程")
    public Result publishCourse(@PathVariable Integer id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return Result.ok();
    }
    //删除课程
    @DeleteMapping("{courseId}")
    @ApiOperation(value ="删除课程")
    @OperationLogDesc(module = "课程模块",description = "删除课程")
    public Result deleteCourse(@PathVariable Integer courseId){
        courseService.removeCourse(courseId);
        return Result.ok();
    }



}

